package nb.diskfilesystem.util;

import javafx.animation.FadeTransition;
import javafx.animation.Interpolator;
import javafx.animation.ParallelTransition;
import javafx.animation.TranslateTransition;
import javafx.scene.control.ContextMenu;
import javafx.util.Duration;

public class AnimationUtil {

    public static void animateContextMenu(boolean show, ContextMenu contextMenu) {
        contextMenu.getScene().getRoot().applyCss();
        contextMenu.getScene().getRoot().layout();

        TranslateTransition translateTransition = new TranslateTransition(Duration.millis(185), contextMenu.getScene().getRoot());
        FadeTransition fadeTransition = new FadeTransition(Duration.millis(185), contextMenu.getScene().getRoot());

        translateTransition.setInterpolator(Interpolator.EASE_BOTH);
        fadeTransition.setInterpolator(Interpolator.EASE_BOTH);

        if (show) {
            translateTransition.setFromY(-contextMenu.getHeight());
            translateTransition.setToY(0);
            fadeTransition.setFromValue(0);
            fadeTransition.setToValue(1);
        } else {
            translateTransition.setFromY(0);
            translateTransition.setToY(-contextMenu.getHeight());
            fadeTransition.setFromValue(1);
            fadeTransition.setToValue(0);
        }

        ParallelTransition parallelTransition = new ParallelTransition(translateTransition, fadeTransition);
        parallelTransition.play();
    }
}